﻿using Game.Actions;
using Game.Common;
using Game.Map;
using Game.Objects;
using Game.PathFinder;

namespace Game.Command.Internal
{
    class MoveUnitCommand : GameCommand
    {
        readonly GamePath _MapPath;
        readonly Vehicle _Vehicle;
        readonly IGameMapView _GameMap;
        
        public MoveUnitCommand(
            IGameMapView gameMap, 
            GamePath path, 
            Vehicle vehicle)
        {
            _GameMap = gameMap;
            _MapPath = path;
            _Vehicle = vehicle;
        }

        public override string Name
        {
            get { return "Move"; }
        }

        protected override ActionResult Execute(
            GameTurn turn,
            Context context)
        {
            var result = new ActionResult(0, "");

            var start = _GameMap.Fields[_MapPath.FirstPoint];
            var last = _GameMap.Fields[_MapPath.LastPoint];
            var finder = FinderFactory.Create(_GameMap, start, last);
            
            var path = finder.Find();

            GameDebug.Assert(path != null);
            if (path == null)
            {
                return result;
            }
            
            GameAction action = new MoveUnit(_GameMap, _Vehicle, path);
            context.ActionExecutor.Execute(_GameMap, context.GameLevels, turn, action);
            
            return result;
        }
    }
}
